/*
 * @Author: your name
 * @Date: 2022-02-03 14:56:22
 * @LastEditTime: 2022-04-11 11:25:00
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /vue2-source-code-analysis/vue2 源码/vue/src/core/global-api/use.js
 */
/* @flow */

import { toArray } from '../util/index'

export function initUse(Vue: GlobalAPI) {
  // Vue.use(plugin)  注册插件
  Vue.use = function (plugin: Function | Object) {
    // Vue.use(VueRouter)
    // 不会重复注册同一个组件
    const installedPlugins =
      this._installedPlugins || (this._installedPlugins = [])
    if (installedPlugins.indexOf(plugin) > -1) {
      return this
    }

    // additional parameters
    // install(Vue)
    const args = toArray(arguments, 1)
    args.unshift(this)
    if (typeof plugin.install === 'function') {
      // plugin 是对象
      plugin.install.apply(plugin, args)
    } else if (typeof plugin === 'function') {
      // plugin 是函数
      plugin.apply(null, args)
    }
    // 将plugin 放入已安装的插件数组中
    installedPlugins.push(plugin)
    return this
  }
}
